const foodHttp = require("../../../utils/foodHttp")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShare: false,
    options: [
      { name: '微信', icon: 'wechat', openType: 'share' },
      { name: '微博', icon: 'weibo' },
      { name: '复制链接', icon: 'link' },
      { name: '分享海报', icon: 'poster' },
      { name: '二维码', icon: 'qrcode' },
    ],
  },

  onClick(event) {
    this.setData({ showShare: true });
  },

  onClose() {
    this.setData({ showShare: false });
  },

  onSelect(event) {
    Toast(event.detail.name);
    this.onClose();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var {
      id
    } = options;
    var step = await foodHttp.getDishDetailHttp(id);
    this.setData({
      steps: step.data.res[0],
      collect: step.data.res[0].collect,
      id,
    })
  },
  async changeCollect(e) {
    var {
      aid
    } = e.currentTarget.dataset;
    console.log(aid);
    var collect = this.data.collect;
    collect = !collect
    console.log(typeof collect);
    wx.request({
      url: 'http://139.9.165.80:8080/api/food/collect',
      method: 'POST',
      data: {
        id: aid,
        collect,
      },
      success: (res => {
        var status = res.data.status
        if (status == 200) {
          wx.showModal({
            confirmColor: '#576B95',
            confirmText: '确定',
            content: '收藏成功',
            showCancel: false,
            title: '提示',
          })
        }else if(status==400){
          wx.showModal({
            confirmColor: '#576B95',
            confirmText: '确定',
            content: '取消收藏',
            showCancel: false,
            title: '提示',
          })
        } 
      })
    })
    this.setData({
      collect,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})